
#include "hecp_get_data.h"
#include <QDebug>
#include <QString>
#include <QMutex>


/*********************************************
 * add by lishuai 2017.05.10
 * Parameters are written into the list.
 * *******************************************/
linE_entity_t* linE_entity_append(linE_entity_t *eroot, const char *key,const char *value)
{
    linE_entity_t *ne,*last;

    ne=(linE_entity_t *)calloc(1,sizeof(linE_entity_t));
    if(ne==NULL){
        //eprintf("calloc FAIL,%m");
        return eroot;
    }

    ne->key=strdup(key);
    ne->value=strdup(value);
    if(ne->key==NULL || ne->value==NULL){

        if(ne->key)free(ne->key);
        if(ne->value)free(ne->value);
        free(ne);
        return eroot;
    }

    if(eroot==NULL)
        return ne;

    last=eroot;
    while(last->next)last=last->next;
    last->next=ne;
    return eroot;
}
/*********************************************
 * add by lishuai 2017.05.10
 * Release the space.
 * *******************************************/
void linE_entity_free(linE_entity_t *eroot)
{
    linE_entity_t *next;

    while(eroot){
        next=eroot->next;
        if(eroot->key)free(eroot->key);
        if(eroot->value)free(eroot->value);
        free(eroot);

        eroot=next;
    }
    return;
}
/*********************************************
 * add by lishuai 2017.05.10
 * Get data by HECP.
 * return NULL:fail.
 * Only support ceo module.
 * *******************************************/
linE_entity_t *Get_Ceo_Data_By_Hecp(int method,char *ip,int port,char *URL,linE_entity_t*param)
{

//    PRTLOG()<<"Post_Ceo_Data_By_Hecp-->METHOD:"<<method;
//    PRTLOG()<<"Post_Ceo_Data_By_Hecp-->ip:"<<ip;
//    PRTLOG()<<"Post_Ceo_Data_By_Hecp-->port:"<<port;
//    PRTLOG()<<"Post_Ceo_Data_By_Hecp-->URL:"<<URL;

    QMutex getdata_mutex;///add by Gaoy---20171214
    int cid = -1;
    struct timeval tout={ 10, 0 };
    int get_times = 0;
    linE_entity_t *ert=NULL;
    getdata_mutex.lock();///add by Gaoy---20171214
get_again:
    if (get_times==2)
    {

        getdata_mutex.unlock();///add by Gaoy---20171214
        return NULL;
    }

    if (get_times > 0)
    {
        //xeLOG_NOTICE("GET_2_ceo again.");
#if defined(BUILD_TARGET_HXSDK_LINE)
        usleep(10000);//10ms
#endif
#ifdef BUILD_TARGET_HXSDK_LINEW
        Sleep(10);
#endif
    }
    get_times++;

    cid = hecp_cli_open_TCP(ip,port);
    if (-1 == cid)
    {
        //xeLOG_ERR("open TCP %s:9823 fail.",ch);
        getdata_mutex.unlock();///add by Gaoy---20171214
        return NULL;
    }
    hecp_cli_set_request_head_URL(cid, URL);
    hecp_cli_set_request_head_method(cid, method);

    while (0x20071126)
    {
        if (param==NULL)
            break;
        hecp_cli_add_request_entity(cid, param->key, param->value);
        param=param->next;
    }

    int retval = hecp_cli_transmit(cid, &tout);
    if (-1 == retval)
    {
        hecp_cli_close(cid);
        //xeLOG_ERR("transmit xeap fail.");
        goto get_again;
    }
    int retval1 = hecp_cli_get_response_head_status_code(cid);

    if (retval1 == STATUS_CODE_200 || retval1 == STATUS_CODE_302 || retval1 == STATUS_CODE_406)///在状态码是302的时候表示成功---modfied by Gaoy--20180122
    {
        hecp_cli_get_response_entity_first(cid);
        while (0x20071126)
        {
            char key[64]="", value[20*1024]="";
            int key_len=sizeof(key), value_len=sizeof(value);

            retval=hecp_cli_get_response_entity_next(cid, key, &key_len, value,&value_len);
            if (retval==-1)
                break;

            ert=linE_entity_append(ert, key, value);
        }

        hecp_cli_close(cid);
        getdata_mutex.unlock();///add by Gaoy---201710214
        return ert;
    }
    else
    {
        hecp_cli_close(cid);
        //xeLOG_INFO("bad response status code %d from %s:9824.",retval,linE_p->ipaddr);
         getdata_mutex.unlock();///add by Gaoy---20171214
        return NULL;
    }
}
/*********************************************
 * modfied by Gaoy 2017.12.29
 * Post data by HECP.
 * return 0:success,-1:fail
 * Only support ceo module.
 * *******************************************/
int Post_Ceo_Data_By_Hecp(int method,char *ip,int port,char *URL,linE_entity_t*param)
{

    int cid = -1;
    int retval = -1;
    struct timeval tout={10,0};
    int post_times=0;
    QMutex setdata_mutex;///add by Gaoy---20171214
    setdata_mutex.lock();///add by Gaoy---20171214
post_again:
    if(post_times==2)
    {
        setdata_mutex.unlock();///add by Gaoy---20171214
        return -1;
    }

    if(post_times>0)
    {
#if defined(HXGUI_RELEASE)
     xeLOG_ERR("POST_2_ceo again");
#else
      PRTLOG()<<"POST_2_ceo again.";
#endif

#if defined(BUILD_TARGET_HXSDK_LINE)
        usleep(10000);//10ms
#endif
#ifdef BUILD_TARGET_HXSDK_LINEW
        Sleep(10);
#endif
    }

    post_times++;
    cid = hecp_cli_open_TCP(ip,port);///创建tcp链接
    if(cid == -1)
    {

#if defined(HXGUI_RELEASE)
     xeLOG_ERR("open TCP :%s port:%d",ip,port);
#else
        PRTLOG()<<"open TCP->"<<ip<<":"<<port<<"fail";///modfied by Gaoy--20171229
#endif
        setdata_mutex.unlock();///add by Gaoy---20171214
        return -1;
    }
    hecp_cli_set_request_head_method(cid,method);
    hecp_cli_set_request_head_URL(cid,URL);
    while(0x20071126)
    {
        if(param==NULL)
            break;
       if(!param->value)///add by Gaoy--20171229
            hecp_cli_add_request_entity(cid,param->key,"NULL");
       else
           hecp_cli_add_request_entity(cid,param->key,param->value);
        param = param->next;
    }
    retval=hecp_cli_transmit(cid,&tout);
    if(retval==-1)
    {
#if defined(HXGUI_RELEASE)
     xeLOG_ERR("transmit  :%s port:%d fail",ip,port);
#else
        PRTLOG()<<"transmit->"<<ip<<":"<<port<<"fail";///modfied by Gaoy--20171229
#endif
        hecp_cli_close(cid);
        return -1;
//        goto post_again;
    }
    retval=hecp_cli_get_response_head_status_code(cid);
    if(retval==STATUS_CODE_200 || retval==STATUS_CODE_302)
    {
        retval=0;
    }
    else
    {
#if defined(HXGUI_RELEASE)
     xeLOG_ERR("bad response status code:%d from:%d",retval,port);
#else
        PRTLOG()<<"bad response status code"<<retval<<"from :"<<port;
#endif
        retval=-1;
    }
    hecp_cli_close(cid);
    setdata_mutex.unlock();///add by Gaoy---20171214
    return retval;
}



